package com.yuwei.module_vehicles.view

import android.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.lib_http.entity.CarPaymentsEntity
import com.bw.lib_http.entity.CarPaymentsEntityItem
import com.yuwei.module_vehicles.R
import com.yuwei.module_vehicles.adapter.CarPaymentsAdapter
import com.yuwei.module_vehicles.databinding.ActivityVehiclesBinding
import com.yuwei.module_vehicles.vm.VehiclesViewModel

/**
 * 智慧园区-02-停车缴费V1.1-20250507-崔志彪
 */
@Route(path = RouterPath.MODULE_CAR_ACTIVITY)
class VehiclesActivity : BaseMvvmActivity<ActivityVehiclesBinding>() {
    private val vm:VehiclesViewModel by viewModels()
    val carPaymentsAdapter = CarPaymentsAdapter()
    val carList = mutableListOf<CarPaymentsEntityItem>()
    override fun initView() {
        //车辆缴费列表
        binding.vehicleRv.adapter = carPaymentsAdapter
        binding.vehicleRv.layoutManager = LinearLayoutManager(this)
        vm.getCarPayments()
        //车辆删除
        carPaymentsAdapter.addChildClickViewIds(R.id.vehicle_edit,R.id.vehicle_delete)
        carPaymentsAdapter.setOnItemChildClickListener { adapter, view, position ->
            when(view.id){
                R.id.vehicle_edit ->{
                    ARouter.getInstance().build(RouterPath.MODULE_CAR_SPACES_ACTIVITY).navigation()
                }
                R.id.vehicle_delete ->{
                    AlertDialog.Builder(this)
                        .setTitle("删除提醒！")
                        .setMessage("确定要删除车辆信息吗?")
                        .setPositiveButton("确定"){dialog,_ ->
                            vm.deleteCar(carPaymentsAdapter.data[position].id)
                            dialog.dismiss()
                        }
                        .setNegativeButton("取消"){dialog,_ ->
                            dialog.dismiss()
                        }
                        .create()
                        .show()
                    return@setOnItemChildClickListener
                }
            }

        }
        //点击添加按钮
        binding.vehicleAddBtn.setOnClickListener {
            ARouter.getInstance().build(RouterPath.MODULE_ADD_CAR_ACTIVITY).navigation()
        }
        //返回
        binding.vehicleBack.setOnClickListener {
            finish()
        }
        //车牌号模糊查询
        binding.vehiclesSearch.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                carPaymentsAdapter.data.clear()
                val tt = s.toString().trim()
                val filterList = carList.filter {
                    it.plateNumber.contains(tt, ignoreCase = true)
                }
                carPaymentsAdapter.data.addAll(filterList)
                carPaymentsAdapter.notifyDataSetChanged()
            }

        })
    }

    override fun loadData() {

        vm.getCarPaymentsSuccess.observe(this){
            carPaymentsAdapter.data.clear()
            carList.clear()
            carList.addAll(it)
            carPaymentsAdapter.data.addAll(carList.reversed())
            carPaymentsAdapter.notifyDataSetChanged()
        }

        vm.getDeleteCarSuccess.observe(this){
            ToastUtils.showShort("删除成功")
            carPaymentsAdapter.data.clear()
            vm.getCarPayments()
            carPaymentsAdapter.notifyDataSetChanged()
        }
    }
    override fun observeDataChange() {

    }

    override fun getLayoutId(): Int = R.layout.activity_vehicles
    override fun onResume() {
        super.onResume()
        vm.getCarPayments()
    }
}